﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using INSTA.GestionEcole.DAL;

namespace INSTA.EcoleGestion.FORMS
{
    public partial class Absence : System.Web.UI.Page
    {
        
           
        


        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                
                    string promoProf = (string) Session["PromoProfesseur"];
                    if (promoProf.Contains("-"))
                    {
                        string[] tab = promoProf.Split(Convert.ToChar("-"));

                        List<string> final = new List<string>();

                        foreach (string s in tab)
                        {
                            if (!final.Contains(s))
                            {
                                final.Add(s);
                            }
                        }
                        string[] tabPromo = (string[]) final.ToArray();
                        for (int i = 0; i < tabPromo.Length; i++)
                        {
                            DropDownList1.Items.Add(tabPromo[i]);
                        }
                    }
                    else
                    {
                        DropDownList1.Items.Add(promoProf);
                    }
                
                BindGrid();
            }


        }

        public void BindGrid()
        {
            using (var context = new GestionInstaEntities())
            {
                

                var query2 = (from p in context.Etudiant
                              where p.Promo == DropDownList1.SelectedItem.Text
                              select new { p.Nom, p.Prenom, p.DateDeNaissance, p.email, p.Promo }).ToList();

                GridView1.DataSource = query2;
             
                GridView1.DataBind();


            }
        }




        protected void ButtonEnvoieAbsenceAdministration(object sender, EventArgs e)
        {
            // bool atLeastOneRowDeleted = false;
            string Date = DateTime.Today.ToString();
            string dat = Date.Substring(0, 10);
            string heure = DateTime.Now.Hour.ToString();
            string minute = DateTime.Now.Minute.ToString();
            string AbsenceMatinOuAprem = null;
            // Access the CheckBox
            foreach (GridViewRow row in GridView1.Rows)
            {
                using (var context = new GestionInstaEntities())
                {
                    // Iterate through the Products.Rows property
                    Absent abs = new Absent();

                    CheckBox cb = (CheckBox)row.FindControl("CheckBoxAbsence");
                    string id = null;
                    if (cb != null && cb.Checked)
                    {


                        string nom = GridView1.Rows[row.RowIndex].Cells[1].Text;
                        string prenom = GridView1.Rows[row.RowIndex].Cells[2].Text;
                        //string dateNaiss = GridView1.Rows[row.RowIndex].Cells[3].Text;
                        string email = GridView1.Rows[row.RowIndex].Cells[4].Text;
                        string promo = GridView1.Rows[row.RowIndex].Cells[5].Text;

                        var query = from etu in context.Etudiant
                                    where (etu.Nom == nom) & (etu.Prenom == prenom)
                                          & (etu.email == email) & (etu.Promo == promo)
                                    //& (etu.DateDeNaissance == dateNaiss)
                                    select etu;




                        foreach (var etudian in query)
                        {
                            id = Convert.ToString(etudian.ID);
                        }

                        if (Convert.ToInt32(heure) < 12)
                        {
                            AbsenceMatinOuAprem = "AM";
                        }
                        else
                        {
                            AbsenceMatinOuAprem = "PM";
                        }

                        //Requete permettant de savoir si l'absence est déja répertorié donc déja checké.

                        var quer = (from a in context.Absent
                                    where (a.Nom == nom) & (a.Prenom == prenom)
                                          & (a.email == email) & (a.Promo == promo)
                                          & (a.DateAbsence.Substring(0, 2) == AbsenceMatinOuAprem)
                                    select a).Count();
                        if (quer == 0)
                        {

                            abs.Nom = nom;
                            abs.Prenom = prenom;
                            abs.email = email;
                            abs.DateAbsence = AbsenceMatinOuAprem + ":   " + dat + "" + heure + "h" + minute + "min";
                            abs.idétudiant = id;
                            abs.Promo = promo;
                            context.Absent.Add(abs);
                            context.SaveChanges();

                        }

                    }


                    else
                    {
                        string nom = GridView1.Rows[row.RowIndex].Cells[1].Text;
                        string prenom = GridView1.Rows[row.RowIndex].Cells[2].Text;
                        //  string dateNaiss = GridView1.Rows[row.RowIndex].Cells[3].Text;
                        string email = GridView1.Rows[row.RowIndex].Cells[4].Text;
                        string promo = GridView1.Rows[row.RowIndex].Cells[5].Text;
                        string MatinOUaprem = "";
                        var query = from etu in context.Etudiant
                                    where (etu.Nom == nom) & (etu.Prenom == prenom)
                                          & (etu.email == email) & (etu.Promo == promo)
                                    select etu;
                        foreach (var etudiant in query)
                        {
                            id = Convert.ToString(etudiant.ID);

                        }
                        var query2 = from ab in context.Absent
                                     where ab.idétudiant == id
                                     select ab;

                        if (Convert.ToInt32(heure) < 12)
                        {
                            AbsenceMatinOuAprem = "AM";
                        }
                        else
                        {
                            AbsenceMatinOuAprem = "PM";
                        }

                        string recupMatinOuAprem = null;

                        foreach (var absence in query2)
                        {
                            recupMatinOuAprem = absence.DateAbsence.Substring(0, 2);


                            if (recupMatinOuAprem == AbsenceMatinOuAprem)
                            {
                                context.Absent.Remove(absence);
                            }
                        }
                        context.SaveChanges();
                    }

                }


                //ListBox1.Items.Add(abs.ToString());
            }
        }

        protected void MiseAjourSelection(object sender, EventArgs e)
        {
            BindGrid();
        }
           
        }
        
    }

